package com.chapter6;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ServletContextTest1 extends HttpServlet {
    private ServletConfig config;
    private ServletContext context;
    @Override
    public void init(ServletConfig config) throws ServletException{
        super.init();
        this.config=config;
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        context=this.config.getServletContext();
        String url=this.context.getInitParameter("url");
        String username=this.context.getInitParameter("username");
        String password=this.context.getInitParameter("password");
        response.setCharacterEncoding("UTF-8");//设置响应的字符集格式为UTF-8
        response.setContentType("text/html");//设置响应正文的MIME类型
        PrintWriter out = response.getWriter();
        //返回一个PrintWriter对象，Servlet使用它来输出字符串形式的正文数据
        //以下为输出的HTML正文数据
        out.println("<HTML>");
        out.println("<HEAD><TITLE>动态生成的HTML文档</TITLE></HEAD>");
        out.println("<BOOY>");
        out.println("获取连接数据库的初始化参数："+"<br/> ");
        out.println("url:" +url+ "<br/>" );
        out.println("username:"+ username +"<br/>");
        out.println("password:"+ password +"<br/>");
        out.println("</BOOY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException{
        doPost(request,response);

    }
}
